﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PLAYXPERT.Interfaces;
using PLAYXPERT.Controls;
using PLAYXPERT;
using PLAYXPERT.Windows;

namespace SkypePXP
{
    public partial class ChatForm : PXPForm
    {
        public string ChatName;
        private BackLib.SkypeWrapper skypeWrapper;
        private SKYPE4COMLib.Chat chat;
        private ImageList imageList;

        public event PopInHandler PopIn;
        public delegate void PopInHandler(string chatName);

        public event AddPeopleHandler AddPeople;
        public delegate void AddPeopleHandler(SKYPE4COMLib.Chat chat);

        public ChatForm(SKYPE4COMLib.Chat chat, BackLib.SkypeWrapper skypeWrapper, ICore icore, ImageList imageList)
        {
            InitializeComponent();
            this.chat = chat;
            ChatName = chat.Name;
            this.skypeWrapper = skypeWrapper;
            this.imageList = imageList;
            listView1.SmallImageList = imageList;
            UpdateChat(chat);
            icore.GetSkinEngine().SkinControls(this, button1);
            icore.GetSkinEngine().SkinControls(this, button2);
            icore.GetSkinEngine().SkinControls(this, button3);
            icore.GetSkinEngine().SkinControls(this, textBox1);
            icore.GetSkinEngine().SkinControls(this, textBox2);
        }

        public SplitContainer GetSplitContainer
        {
            get
            {
                return splitContainer2;
            }
        }

        public void UpdateChat(SKYPE4COMLib.Chat chat)
        {
            listView1.Items.Clear();
            for (int i = 0; i < chat.Members.Count; i ++)
            {
                try
                {
                    string displayText = skypeWrapper.GetDisplayName(skypeWrapper.UserFromHandle(chat.Members[i].Handle));
                    int displayLength = 20;
                    if (displayText.Length > displayLength)
                    {
                        displayText = displayText.Remove(displayLength, displayText.Length - displayLength);
                        displayText += "..";
                    }
                    ListViewItem item = new ListViewItem(displayText, (int)skypeWrapper.GetOnlineStatusFriendly(chat.Members[i]));
                    item.Tag = chat.Members[i];
                    listView1.Items.Add(item);
                }
                catch { }
            }
            
            this.Text = skypeWrapper.GetChatName(chat);
        }

        public void UpdateUser(SKYPE4COMLib.User user)
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                ListViewItem item = listView1.Items[0];
                if (item.Tag.ToString() == user.Handle)
                {
                    item = new ListViewItem(skypeWrapper.GetDisplayName(user), (int)skypeWrapper.GetOnlineStatusFriendly(user));
                    return;
                }
            }
        }

        public void UpdateMessages(SKYPE4COMLib.ChatMessage message)
        {
            textBox2.Text += Environment.NewLine + skypeWrapper.GetDisplayName(skypeWrapper.UserFromHandle(message.FromHandle)) + ": " + message.Body;
            textBox2.SelectionStart = textBox2.Text.Length;
            textBox2.ScrollToCaret();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TogglePop();
        }

        public void TogglePop()
        {
            if (this.Visible)
            {
                this.Visible = false;
                button2.Text = "Pop Out";
                PopIn(ChatName);
            }
            else
            {
                this.Show();
                button2.Text = "Pop In";
                this.Controls.Add(splitContainer2);
                splitContainer2.Dock = DockStyle.Fill;
            }
        }

        private void ChatForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TogglePop();
            e.Cancel = true;
        }

        bool handleNextKey = false;

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (handleNextKey)
            {
                e.Handled = true;
                handleNextKey = false;
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !e.Shift && !e.Control)
            {
                button1.PerformClick();
                handleNextKey = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                chat.SendMessage(textBox1.Text);
            }
            textBox1.Clear();
        }

        private void ChatForm_VisibleChanged(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void addPeopleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddPeople(chat);
        }

        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            button3.ContextMenuStrip.Show(button3, e.Location);
        }
    }
}
